package com.yang.mall.product.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.hutool.core.bean.BeanUtil;
import com.yang.mall.product.model.vo.CategoryTreeNodeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yang.mall.product.entity.CategoryEntity;
import com.yang.mall.product.service.CategoryService;
import com.yang.common.utils.PageUtils;
import com.yang.common.utils.R;



/**
 * 商品三级分类
 *
 * @author lzy
 * @email 3079314177@qq.com
 * @date 2024-05-24 18:45:00
 */
@Slf4j
@RestController
@RequestMapping("/product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 获取树形结构的分类集合
     */
    @GetMapping("/list/tree")
    public R listWithTree() {
        log.info("获取树形分类...");
        List<CategoryTreeNodeVO> categoryTrees = categoryService.listWithTree();
        return R.ok().put("data",categoryTrees);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        log.info("获取分类list...\n\t参数：{}",params);
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
        log.info("获取分类{}的信息...");
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
        log.info("保存分类 {} 的信息",category);
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
        log.info("更新分类{}",category);
		categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 批量修改分类的顺序
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryTreeNodeVO[] categoryTreeNodeVOS){
        log.info("更新分类顺序{}",categoryTreeNodeVOS);
        List<CategoryEntity> list = Arrays.stream(categoryTreeNodeVOS)
                .map(cNode -> BeanUtil.copyProperties(cNode, CategoryEntity.class))
                .collect(Collectors.toList());
        categoryService.updateBatchById(list);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        log.info("删除分类{}",catIds);
		categoryService.removeByIds(Arrays.asList(catIds));

        return R.ok();
    }

}
